/**
 * @desc 文章正文
 */

const moment = require("moment")

module.exports = (sequelize, dataTypes) => {
    const Article = sequelize.define(
        "article", // 实际对应数据库表名
        {
            id: { type: dataTypes.INTEGER(11), primaryKey: true, autoIncrement: true },
            title: { type: dataTypes.STRING(255), allowNull: false },
            content: { type: dataTypes.TEXT },
            createdAt: {
                type: dataTypes.DATE,
                defaultValue: dataTypes.NOW,
                get() {
                    return moment(this.getDataValue("createdAt")).format("YYYY-MM-DD HH:mm:ss")
                }
            },
            updatedAt: {
                type: dataTypes.DATE,
                defaultValue: dataTypes.NOW,
                get() {
                    return moment(this.getDataValue("updatedAt")).format("YYYY-MM-DD HH:mm:ss")
                }
            }
        },
        {
            timestamps: true
        }
    )

    // 这里的"models"其实来自"/models/index.js/db"
    Article.associate = models => {
        Article.hasMany(models.tag)
    }

    return Article
}
